package org.irri.smta.ui.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

import org.irri.smta.common.Constants;
import org.irri.smta.domain.Material;
import org.irri.smta.domain.SmtaReport;
import org.irri.smta.service.IMaterialService;
import org.irri.smta.service.ISmtaReportService;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Named("materialBean")
@SessionScoped
public class MaterialBean extends SelectableTableBeanBase<Material> {

	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory
			.getLogger(MaterialBean.class);

	private static final String PARAM_ID = "id";

	@Inject
	@Named("materialService")
	private IMaterialService<Material, Long> materialService;

	@Inject
	@Named("smtaReportService")
	private ISmtaReportService<SmtaReport, Long> smtaReportService;

	private LazyDataModel<Material> materialDataModel = null;
	private Material selectedMaterial = new Material();
	private Material newMaterial = new Material();
	private String smtaId = null;

	public MaterialBean() {
		super();
		setViewName(Constants.MATERIAL_VIEW_NAME);
	}

	@PostConstruct
	public void init() {
		LOGGER.debug("init()");
		selectedMaterial = new Material();
		newMaterial = new Material();
	}

	public IMaterialService<Material, Long> getMaterialService() {
		return materialService;
	}

	public String getSmtaId() {
		return smtaId;
	}

	public void setSmtaId(String smtaId) {
		this.smtaId = smtaId;
	}

	public Material getSelectedMaterial() {
		if (selectedMaterial == null) {
			selectedMaterial = new Material();
		}
		return selectedMaterial;
	}

	public void setSelectedMaterial(Material material) {
		this.selectedMaterial = material;
	}

	public Material getNewMaterial() {
		if (newMaterial == null) {
			newMaterial = new Material();
		}
		return newMaterial;
	}

	public void setNewMaterial(Material newMaterial) {
		this.newMaterial = newMaterial;
	}

	public LazyDataModel<Material> getAllMaterials() {
		materialDataModel = new LazyDataModel<Material>() {
			private static final long serialVersionUID = 1L;

			@Override
			public List<Material> load(int rowIndex, int pageSize,
					String sortField, SortOrder sortOrder,
					Map<String, String> filters) {
				LOGGER.debug("List<Material> load(int rowIndex, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)");
				List<Material> data = new ArrayList<Material>();
				if (rowIndex < 0) {
					rowIndex = 0;
				}

				Long id = 0L;
				FacesContext facesContext = FacesContext.getCurrentInstance();
				if (facesContext != null) {
					HttpSession session = (HttpSession) facesContext
							.getExternalContext().getSession(false);
					if (session != null) {
						Object attr = session.getAttribute(PARAM_ID);
						if (attr != null) {
							id = Long.valueOf(attr.toString());
						}
					}
				}
				
				SmtaReport smtaReport = smtaReportService.findById(id);
				if (smtaReport != null) {
					List<Material> materials = smtaReport.getMaterials();
					if (materials != null) {
						data.addAll(materials);
					}
				}
				
				// rowCount
				Long rowCount = Long.valueOf(data.size());
				Integer dataSize = rowCount != null ? rowCount.intValue()
						: null;
				this.setRowCount(dataSize);
				// pageSize
				this.setPageSize(dataSize);

				return data;
			}
		};
		return materialDataModel;
	}

	@Override
	public Object getRowKey(Material object) {
		return object.getId();
	}

	@Override
	public Material getRowData(String rowKey) {
		return materialService.findById(new Long(rowKey));
	}

}
